
import json
import collections
import os, re

#============================================================================
def verb_present_participle(word):

    # 以ie结尾的名词, 一般将ie改为y, 再加ing
    if re.search("ie$", word):
        return re.sub("ie$", "ying", word), 3

    # 以不发音的字母e结尾的动词, 先去掉e再加ing
    if re.search("e$", word):
        return re.sub("e$", "ing", word), 2

    ## 以'c'结尾的动词, 通常加'king'构成现在分词
    #if re.search("c$", word):
    #    return re.sub("c$", "king", word), 1

    ## 以一个元音字母和一个辅音结尾的重读音节结尾的动词, 要双写末尾一个辅音字母, 再加ing
    ## 注意, 如词尾的辅音字母是'w','x'和'y', 就不需要双写结尾的字母
    ## 英国英语中, 双音节动词以一个元音字母加一个'l'字母结尾时, 要双写末尾一个辅音字母. 而美国英语中, 可不双写.
    #if re.search("[aeiou][^aeiouwxy]$", word):
    #    return re.sub("$", word[-1]+"ing", word), 4

    # 一般情况下在动词后加ing
    return word+"ing", 0

#============================================================================
# 某些动词的过去式或过去分词不止一个
def verb_past_tense(word ):
    # 以不发音的字母e结尾的动词, 先去掉e再加ed
    if re.search("e$", word):
        return re.sub("e$", "ed", word) , 0

    # 以辅音字母加'y '结尾的动词构成过去式时用'ied '代替'y
    if re.search("[^aeiou]y$", word):
        return re.sub("y$", "ied", word), 1

    ## c结尾的动词, 通常加'ked '构成过去式
    #if re.search("c$", word):
    #    return  re.sub("c$", "ked", word), 2
    #
    ## 动词原形是单音节词, 词尾是一个元音字母加一个辅音字母, 在加'ing '或'ed前先双写该辅音字母。
    ## 有些双音节动词的第二个音节以一个元音字母加一个辅音字母结尾, 同时又要重读, 在加'ing '或'ed前, 这类双音节动词的最后一个辅音字母也要双写。
    ## 英国英语中, 双音节动词以一个元音字母加一个'l'字母结尾时, 要双写末尾一个辅音字母. 而美国英语中, 可不双写.
    #if re.search("[aeiou][^aeiouwxy]$", word):
    #    return re.sub("$", word[-1]+"ed", word), 3

    # 一般情况下在动词后加ed
    return word+"ed", 4

#============================================================================
def verb_Singular_From_in_Third_Personal(word):

    # 形以辅音字母加'y '结尾的动词, 用'ies '代替'y '构成's '形式,
    if re.search("[^aeiou]y$", word):
        return re.sub("y$", "ies", word), 0

    # sh, ch, ss, x , zz, o 结尾的动词, 原形后加es
    if re.search("sh$|ch$|ss$|x$|zz$|o$", word):
        return word + "es", 1

    # s结尾的单音节词, 原形后加es
    if re.search("s$", word):
        return word + "es", 2

    # 多数动词的, s形式由原形加词尾, s构成
    return word+"s", 3

#============================================================================
f = open("e:\\w\\forms-verb.csv", 'r' ,encoding='utf8')
fo = open("e:\\w\\forms-verb-irregular.csv", 'w' ,encoding='utf8')
fword = open("e:\\w\\verb_list.csv", 'w' ,encoding='utf8')


words = {}

for line in f.readlines():
    items=re.split("\|",line)
    word                  =  items[0].strip() if len(items) >= 1 else None
    present_participle    =  items[1].strip() if len(items) >= 2  else ""
    past_participle       =  items[2].strip() if len(items) >= 3  else ""
    past_tense            =  items[3].strip() if len(items) >= 4  else ""
    third_person_singular =  items[4].strip() if len(items) >= 5  else ""
    
    fword.write(word+"\n")

    form_from_book = present_participle
    if len(present_participle) :
        word_normal_form, kind = verb_present_participle(word)
        p_items =re.split(",",present_participle)
        found=False
        for pi in p_items:
            if  word_normal_form == pi:
                #fo.write("~ "+str(kind)+"\n")
                found=True
                pass
                break

        if found is False:
            #print(word +"|" + present_participle +"|"+ word_normal_form)
            #fo.write(str(kind)+"\n")
            pass
        else:
            present_participle="_"

    form_from_book = past_participle
    if len(form_from_book) :
        word_normal_form, kind = verb_past_tense(word)
        p_items =re.split(",",form_from_book)
        found=False
        for pi in p_items:
            if  word_normal_form == pi:
                #fo.write("~ "+str(kind)+"\n")
                found=True
                pass
                break

        if found is False:
            #print(word +"|" + form_from_book +"|"+ word_normal_form)
            #fo.write(str(kind)+"\n")
            pass
        else:
            past_participle="_"


    form_from_book = past_tense
    if len(form_from_book) :
        word_normal_form, kind = verb_past_tense(word)
        p_items =re.split(",",form_from_book)
        found=False
        for pi in p_items:
            if  word_normal_form == pi:
                #fo.write("~ "+str(kind)+"\n")
                found=True
                pass
                break

        if found is False:
            #print(word +"|" + form_from_book +"|"+ word_normal_form)
            #fo.write(str(kind)+"\n")
            pass
        else:
            past_tense="_"

    form_from_book = third_person_singular
    if len(form_from_book) :
        word_normal_form, kind = verb_Singular_From_in_Third_Personal(word)
        p_items =re.split(",",form_from_book)
        found=False
        for pi in p_items:
            if  word_normal_form == pi:
                #fo.write("~ "+str(kind)+"\n")
                found=True
                pass
                break

        if found is False:
            #print(word +"|" + form_from_book +"|"+ word_normal_form)
            #fo.write(str(kind)+"\n")
            pass
        else:
            third_person_singular="_"
    if (present_participle and present_participle != "_")  or (past_participle and past_participle != "_")  or(past_tense and past_tense != "_")  or(third_person_singular and third_person_singular != "_"):
        fo.write(word+"|"+"{}|{}|{}|{}\n".format(present_participle,past_participle,past_tense,third_person_singular ))
        pass
    else:
        #fo.write("{}|{}|{}|{}\n".format(present_participle,past_participle,past_tense,third_person_singular ))
        pass
fo.close()

fword.close()

